Code
# libraries
import pandas as pd
import os
import geopandas as gpd
from geopandas.tools import geocodeWe load the Python packages needed for our analysis.
# libraries
import pandas as pd
import os
import geopandas as gpd
from geopandas.tools import geocodeWe load the data from a text file. We use read the file as a dataframe using a the method in pandas that reads fixed-width files, pandas.read_fwf. See pandas.read_fwf — pandas 2.3.3 documentation for more information. In the method pandas.read_fwf, we use the arguments:
width = [5, 70] to specify the column widths in terms of number of characters,header=0 to specifify that there is no header information in the file, andnames=["site", "description"] to give the resulting dataframe the column names "site" and "description".flock_text_file_location = "Flock Sites.txt"
flock_df = pd.read_fwf(
flock_text_file_location,
widths = [5, 70],
header = 0,
names = ["site", "description"]
)
flock_df| site | description | |
|---|---|---|
| 0 | P#002 | Rindge Ave @ Alewife Brook Pkwy (solar power, ... |
| 1 | P#003 | Cambridge St @ Hampshire St (solar power, city... |
| 2 | F#004 | Main St @ Osborn St WB (AC power, city light p... |
| 3 | P#005 | Cardinal Medeiros Ave@ Binney St (AC power, ci... |
| 4 | P#006 | Galileo Galilei Way@ Broadway (solar power, ci... |
| 5 | P#007 | Memorial Dr@ JFK St (solar power, city light p... |
| 6 | P#008 | Rindge Ave @ Pemberton St (solar power, city l... |
| 7 | P#009 | Massachusetts Ave @ Memorial Dr (AC power, cit... |
| 8 | P#010 | Windsor St@ Hampshire St (solar power, city li... |
| 9 | P#011 | Cambridge St@ N First St (AC power, city light... |
| 10 | P#012 | Third St @ Gore St (solar power, surface wall ... |
| 11 | P#013 | Hampshire St@ Cambridge St (solar power, city ... |
| 12 | P#014 | Main St @ Broadway (solar power, city light pole) |
| 13 | P#015 | Binney St@ Third St (AC power, city light pole) |
| 14 | P#016 | Galileo Galilei Way@ Broadway (solar power, ci... |
We loop through all of the columns of the dataframe and trim the whitespace.
for col in flock_df.columns:
flock_df[col].str.strip()We remove the close brackets, and then we split of the characters @ and open brackets ( in order to create columns that extract parts of the flock site address and extra descriptions for the flock sites. In order to create flock site full addresses for a geocoding search we combine the first and seconds parts of the location together with the suffix , Cambridge, MASS, USA.
temp_description_df = flock_df["description"].str.replace(")", "").str.split("@|\(", expand = True, n=2)
temp_description_df| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | Rindge Ave | Alewife Brook Pkwy | solar power, city traffic signal |
| 1 | Cambridge St | Hampshire St | solar power, city light pole |
| 2 | Main St | Osborn St WB | AC power, city light pole |
| 3 | Cardinal Medeiros Ave | Binney St | AC power, city light pole |
| 4 | Galileo Galilei Way | Broadway | solar power, city light pole |
| 5 | Memorial Dr | JFK St | solar power, city light pole |
| 6 | Rindge Ave | Pemberton St | solar power, city light pole |
| 7 | Massachusetts Ave | Memorial Dr | AC power, city light pole |
| 8 | Windsor St | Hampshire St | solar power, city light pole |
| 9 | Cambridge St | N First St | AC power, city light pole |
| 10 | Third St | Gore St | solar power, surface wall mount |
| 11 | Hampshire St | Cambridge St | solar power, city light pole |
| 12 | Main St | Broadway | solar power, city light pole |
| 13 | Binney St | Third St | AC power, city light pole |
| 14 | Galileo Galilei Way | Broadway | solar power, city light pole |
flock_df = flock_df.assign(
flock_site_address = temp_description_df[0].str.strip().str.cat(
temp_description_df[1].str.strip(),
sep = " & "
).str.strip() + ", Cambridge, Massachusetts, USA",
flock_site_description = temp_description_df[2].str.strip()
)
# delete object
del temp_description_df
flock_df| site | description | flock_site_address | flock_site_description | |
|---|---|---|---|---|
| 0 | P#002 | Rindge Ave @ Alewife Brook Pkwy (solar power, ... | Rindge Ave & Alewife Brook Pkwy, Cambridge, Ma... | solar power, city traffic signal |
| 1 | P#003 | Cambridge St @ Hampshire St (solar power, city... | Cambridge St & Hampshire St, Cambridge, Massac... | solar power, city light pole |
| 2 | F#004 | Main St @ Osborn St WB (AC power, city light p... | Main St & Osborn St WB, Cambridge, Massachuset... | AC power, city light pole |
| 3 | P#005 | Cardinal Medeiros Ave@ Binney St (AC power, ci... | Cardinal Medeiros Ave & Binney St, Cambridge, ... | AC power, city light pole |
| 4 | P#006 | Galileo Galilei Way@ Broadway (solar power, ci... | Galileo Galilei Way & Broadway, Cambridge, Mas... | solar power, city light pole |
| 5 | P#007 | Memorial Dr@ JFK St (solar power, city light p... | Memorial Dr & JFK St, Cambridge, Massachusetts... | solar power, city light pole |
| 6 | P#008 | Rindge Ave @ Pemberton St (solar power, city l... | Rindge Ave & Pemberton St, Cambridge, Massachu... | solar power, city light pole |
| 7 | P#009 | Massachusetts Ave @ Memorial Dr (AC power, cit... | Massachusetts Ave & Memorial Dr, Cambridge, Ma... | AC power, city light pole |
| 8 | P#010 | Windsor St@ Hampshire St (solar power, city li... | Windsor St & Hampshire St, Cambridge, Massachu... | solar power, city light pole |
| 9 | P#011 | Cambridge St@ N First St (AC power, city light... | Cambridge St & N First St, Cambridge, Massachu... | AC power, city light pole |
| 10 | P#012 | Third St @ Gore St (solar power, surface wall ... | Third St & Gore St, Cambridge, Massachusetts, USA | solar power, surface wall mount |
| 11 | P#013 | Hampshire St@ Cambridge St (solar power, city ... | Hampshire St & Cambridge St, Cambridge, Massac... | solar power, city light pole |
| 12 | P#014 | Main St @ Broadway (solar power, city light pole) | Main St & Broadway, Cambridge, Massachusetts, USA | solar power, city light pole |
| 13 | P#015 | Binney St@ Third St (AC power, city light pole) | Binney St & Third St, Cambridge, Massachusetts... | AC power, city light pole |
| 14 | P#016 | Galileo Galilei Way@ Broadway (solar power, ci... | Galileo Galilei Way & Broadway, Cambridge, Mas... | solar power, city light pole |
geo = geocode(
flock_df["flock_site_address"],
provider="arcgis",
user_agent="flock_sites_analysis_ipynb",
timeout=10
)
geo.head()| geometry | address | |
|---|---|---|
| 0 | POINT (-71.14047 42.39407) | Rindge Ave & Alewife Brook Pkwy, Cambridge, Ma... |
| 1 | POINT (-71.10132 42.37379) | Cambridge St & Hampshire St, Cambridge, Massac... |
| 2 | POINT (-71.09527 42.3631) | Main St & Osborn St, Cambridge, Massachusetts,... |
| 3 | POINT (-71.09094 42.36773) | Cardinal Medeiros Ave & Binney St, Cambridge, ... |
| 4 | POINT (-71.08951 42.36475) | Galileo Galilei Way & Broadway, Cambridge, Mas... |
flock_gdf = geo.join(flock_df)
# we no longer need flock_df
del flock_df
flock_gdf| geometry | address | site | description | flock_site_address | flock_site_description | |
|---|---|---|---|---|---|---|
| 0 | POINT (-71.14047 42.39407) | Rindge Ave & Alewife Brook Pkwy, Cambridge, Ma... | P#002 | Rindge Ave @ Alewife Brook Pkwy (solar power, ... | Rindge Ave & Alewife Brook Pkwy, Cambridge, Ma... | solar power, city traffic signal |
| 1 | POINT (-71.10132 42.37379) | Cambridge St & Hampshire St, Cambridge, Massac... | P#003 | Cambridge St @ Hampshire St (solar power, city... | Cambridge St & Hampshire St, Cambridge, Massac... | solar power, city light pole |
| 2 | POINT (-71.09527 42.3631) | Main St & Osborn St, Cambridge, Massachusetts,... | F#004 | Main St @ Osborn St WB (AC power, city light p... | Main St & Osborn St WB, Cambridge, Massachuset... | AC power, city light pole |
| 3 | POINT (-71.09094 42.36773) | Cardinal Medeiros Ave & Binney St, Cambridge, ... | P#005 | Cardinal Medeiros Ave@ Binney St (AC power, ci... | Cardinal Medeiros Ave & Binney St, Cambridge, ... | AC power, city light pole |
| 4 | POINT (-71.08951 42.36475) | Galileo Galilei Way & Broadway, Cambridge, Mas... | P#006 | Galileo Galilei Way@ Broadway (solar power, ci... | Galileo Galilei Way & Broadway, Cambridge, Mas... | solar power, city light pole |
| 5 | POINT (-71.12246 42.36981) | Memorial Dr & JFK St, Cambridge, Massachusetts... | P#007 | Memorial Dr@ JFK St (solar power, city light p... | Memorial Dr & JFK St, Cambridge, Massachusetts... | solar power, city light pole |
| 6 | POINT (-71.1253 42.39258) | Rindge Ave & Pemberton St, Cambridge, Massachu... | P#008 | Rindge Ave @ Pemberton St (solar power, city l... | Rindge Ave & Pemberton St, Cambridge, Massachu... | solar power, city light pole |
| 7 | POINT (-71.09261 42.35729) | Massachusetts Ave & Memorial Dr, Cambridge, Ma... | P#009 | Massachusetts Ave @ Memorial Dr (AC power, cit... | Massachusetts Ave & Memorial Dr, Cambridge, Ma... | AC power, city light pole |
| 8 | POINT (-71.09478 42.36877) | Windsor St & Hampshire St, Cambridge, Massachu... | P#010 | Windsor St@ Hampshire St (solar power, city li... | Windsor St & Hampshire St, Cambridge, Massachu... | solar power, city light pole |
| 9 | POINT (-71.07693 42.37062) | Cambridge St & 1st St, Cambridge, Massachusett... | P#011 | Cambridge St@ N First St (AC power, city light... | Cambridge St & N First St, Cambridge, Massachu... | AC power, city light pole |
| 10 | POINT (-71.0797 42.37172) | 3rd St & Gore St, Cambridge, Massachusetts, 02141 | P#012 | Third St @ Gore St (solar power, surface wall ... | Third St & Gore St, Cambridge, Massachusetts, USA | solar power, surface wall mount |
| 11 | POINT (-71.10132 42.37379) | Hampshire St & Cambridge St, Cambridge, Massac... | P#013 | Hampshire St@ Cambridge St (solar power, city ... | Hampshire St & Cambridge St, Cambridge, Massac... | solar power, city light pole |
| 12 | POINT (-71.08211 42.3621) | Main St & Broadway, Cambridge, Massachusetts, ... | P#014 | Main St @ Broadway (solar power, city light pole) | Main St & Broadway, Cambridge, Massachusetts, USA | solar power, city light pole |
| 13 | POINT (-71.08247 42.36561) | Binney St & 3rd St, Cambridge, Massachusetts, ... | P#015 | Binney St@ Third St (AC power, city light pole) | Binney St & Third St, Cambridge, Massachusetts... | AC power, city light pole |
| 14 | POINT (-71.08951 42.36475) | Galileo Galilei Way & Broadway, Cambridge, Mas... | P#016 | Galileo Galilei Way@ Broadway (solar power, ci... | Galileo Galilei Way & Broadway, Cambridge, Mas... | solar power, city light pole |
We write the flock site data with geocoded information to file.
csv_output_folder = "./CSV Files"
flock_gdf.to_csv(
os.path.join(
csv_output_folder,
"flock_sites_geometries.csv"
),
index = False
)We use an equal area projection for accuracy. We use the term massachusetts as the search term in EPSG.io Coordinate Systems Worldwide to give EPSG:26987
We consider a buffer of radius 300 feet or 91.4 metres for each Flock Site.
# projection constant
epsg_constant = 26987
# convert using equal area projection
flock_gdf= flock_gdf.to_crs(epsg=epsg_constant)
# buffer length
buffer_length_in_meters = 91.44
# add buffer to geometries
flock_gdf['geometry'] = flock_gdf.geometry.buffer(buffer_length_in_meters)
flock_gdf| geometry | address | site | description | flock_site_address | flock_site_description | |
|---|---|---|---|---|---|---|
| 0 | POLYGON ((447349.252 155036.532, 447348.812 15... | Rindge Ave & Alewife Brook Pkwy, Cambridge, Ma... | P#002 | Rindge Ave @ Alewife Brook Pkwy (solar power, ... | Rindge Ave & Alewife Brook Pkwy, Cambridge, Ma... | solar power, city traffic signal |
| 1 | POLYGON ((450557.156 152760.721, 450556.716 15... | Cambridge St & Hampshire St, Cambridge, Massac... | P#003 | Cambridge St @ Hampshire St (solar power, city... | Cambridge St & Hampshire St, Cambridge, Massac... | solar power, city light pole |
| 2 | POLYGON ((451047.796 151569.637, 451047.356 15... | Main St & Osborn St, Cambridge, Massachusetts,... | F#004 | Main St @ Osborn St WB (AC power, city light p... | Main St & Osborn St WB, Cambridge, Massachuset... | AC power, city light pole |
| 3 | POLYGON ((451407.899 152081.398, 451407.458 15... | Cardinal Medeiros Ave & Binney St, Cambridge, ... | P#005 | Cardinal Medeiros Ave@ Binney St (AC power, ci... | Cardinal Medeiros Ave & Binney St, Cambridge, ... | AC power, city light pole |
| 4 | POLYGON ((451523.211 151749.047, 451522.771 15... | Galileo Galilei Way & Broadway, Cambridge, Mas... | P#006 | Galileo Galilei Way@ Broadway (solar power, ci... | Galileo Galilei Way & Broadway, Cambridge, Mas... | solar power, city light pole |
| 5 | POLYGON ((448813.069 152330.46, 448812.629 152... | Memorial Dr & JFK St, Cambridge, Massachusetts... | P#007 | Memorial Dr@ JFK St (solar power, city light p... | Memorial Dr & JFK St, Cambridge, Massachusetts... | solar power, city light pole |
| 6 | POLYGON ((448597.443 154861.876, 448597.003 15... | Rindge Ave & Pemberton St, Cambridge, Massachu... | P#008 | Rindge Ave @ Pemberton St (solar power, city l... | Rindge Ave & Pemberton St, Cambridge, Massachu... | solar power, city light pole |
| 7 | POLYGON ((451261.886 150922.732, 451261.446 15... | Massachusetts Ave & Memorial Dr, Cambridge, Ma... | P#009 | Massachusetts Ave @ Memorial Dr (AC power, cit... | Massachusetts Ave & Memorial Dr, Cambridge, Ma... | AC power, city light pole |
| 8 | POLYGON ((451091.878 152198.58, 451091.437 152... | Windsor St & Hampshire St, Cambridge, Massachu... | P#010 | Windsor St@ Hampshire St (solar power, city li... | Windsor St & Hampshire St, Cambridge, Massachu... | solar power, city light pole |
| 9 | POLYGON ((452563.678 152394.273, 452563.237 15... | Cambridge St & 1st St, Cambridge, Massachusett... | P#011 | Cambridge St@ N First St (AC power, city light... | Cambridge St & N First St, Cambridge, Massachu... | AC power, city light pole |
| 10 | POLYGON ((452336.878 152518.331, 452336.437 15... | 3rd St & Gore St, Cambridge, Massachusetts, 02141 | P#012 | Third St @ Gore St (solar power, surface wall ... | Third St & Gore St, Cambridge, Massachusetts, USA | solar power, surface wall mount |
| 11 | POLYGON ((450557.156 152760.721, 450556.716 15... | Hampshire St & Cambridge St, Cambridge, Massac... | P#013 | Hampshire St@ Cambridge St (solar power, city ... | Hampshire St & Cambridge St, Cambridge, Massac... | solar power, city light pole |
| 12 | POLYGON ((452131.18 151450.867, 452130.739 151... | Main St & Broadway, Cambridge, Massachusetts, ... | P#014 | Main St @ Broadway (solar power, city light pole) | Main St & Broadway, Cambridge, Massachusetts, USA | solar power, city light pole |
| 13 | POLYGON ((452104.205 151841.407, 452103.765 15... | Binney St & 3rd St, Cambridge, Massachusetts, ... | P#015 | Binney St@ Third St (AC power, city light pole) | Binney St & Third St, Cambridge, Massachusetts... | AC power, city light pole |
| 14 | POLYGON ((451523.211 151749.047, 451522.771 15... | Galileo Galilei Way & Broadway, Cambridge, Mas... | P#016 | Galileo Galilei Way@ Broadway (solar power, ci... | Galileo Galilei Way & Broadway, Cambridge, Mas... | solar power, city light pole |
flock_gdf.explore(color = "red")We load the building shapefiles for Cambridge, Mass., sourced from Buildings - GIS - City of Cambridge, Massachusetts.
To save space and memory in our analysis, we only consider the columns for the building identifier “BldgID”, “TYPE” of building and “geommetry” i.e. shape information.
buildings_gdf = gpd.read_file(
"./Data/BASEMAP_Buildings.shp/BASEMAP_Buildings.shp",
)We convert the buildings information to use the same equal area projection as epsg_constant = 26987
buildings_gdf = buildings_gdf.to_crs(epsg=epsg_constant)# convert column to string
buildings_gdf["BldgID"] = buildings_gdf["BldgID"].astype(str)buildings_gdf.explore(color = "brown")